package com.rainner.admin.action;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.rainner.admin.action.base.JsonAction;
import com.rainner.admin.server.ManagerServer;

@Results({ 
	@Result(type = "freemarker", name = "INDEX", location = "/WEB-INF/content/index.ftl"),
	@Result(type = "redirect", name = "LOGIN", location = "main/index.html"),
	@Result(type = "redirect", name = "OUTLOGIN", location = "/") 
	})
public class IndexAction extends JsonAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ManagerServer server = new ManagerServer();
	private String username;
	private String password;

	@Action("/index")
	public String index() {
		return "INDEX";
	}

	@Action("/login")
	public String login() throws Exception {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (server.checkManage(username, password)) {
			session.setAttribute("username", username);
			return "LOGIN";
		} else {
			return "OUTLOGIN";
		}
	}
	
	@Action("/login/check")
	public String loginCheck() throws Exception {
		if (server.checkManage(username, password)) {
			this.result.setState(true);
		} else {
			this.result.setState(false);
		}
		map.put("message", this.result);
		return MAP;
	}
	
	@Action("/outlogin")
	public String outLogin(){
		HttpSession session = ServletActionContext.getRequest().getSession();
		session.removeAttribute("username");
		return "OUTLOGIN";
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
